<template>
  <headerTopView
    :userN="userN"
    :appName="appName"
    :icon="icon"
    :editPwddialog="editPwddialog"
    :addPwdform="addPwdform"
    @outSide="outSide"
    @editPwdClose="editPwdClose"
    @editPwdCancel="editPwdCancel"
    @editPwdKeep="editPwdKeep"
    ref="headerTop"
  ></headerTopView>
</template>

<script>
  import headerTopView from './headerTopView';
  import {localUitl} from '@/utils/localUitl'

  export default {

    components: {
      headerTopView
      // name
    },
    name: "headerTop",
    props: {
      appName: {
        type: String,
        default: ""
      },
      icon: {
        type: String,
        default: ""
      },
      userN: {
        type: Object,
        default: {}
      }
      // msg: String //  [name: typeof]
    },
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        editPwddialog: false,
        addPwdform: {
          oldPwd: "",
          newPwd: "",
          againPwd: ""
        }
        // userN: {}
      }
    },
    created() {

    },
    mounted() {

    },
    methods: {
      outSide(command) {
        switch (command) {
          case "editPwd":
            this.editpwd();
            break;
          case "goOut":
            this.$router.push('/navigationPage');
            break;
        }
      },
      // 修改密码
      editpwd() {
        this.editPwddialog = true;
      },
      // 修改密码关闭的回调
      editPwdClose() {
        this.editPwddialog = false;
        this.$refs.headerTop.$refs.editPwdform.resetFields();
      },
      // 弹窗点击取消
      editPwdCancel() {
        this.editPwddialog = false;
        this.$refs.headerTop.$refs.editPwdform.resetFields();
      },
      // 弹窗点击保存
      editPwdKeep() {
        this.$refs.headerTop.$refs.editPwdform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            let addPwdform = this.addPwdform;
            let userN = this.userN;
            if (addPwdform.newPwd != addPwdform.againPwd) {
              this.$message({
                type: 'error',
                message: '密码不一致!'
              });
              return
            }

            let obj = {
              loginname: userN.name,
              oldPwd: addPwdform.oldPwd,
              newPwd: addPwdform.newPwd
            };

            _https.post.editUserPwd(obj).then(res => {
              if (res.data.code == 0) {
                this.$message({
                  type: 'success',
                  message: `${res.data.message}，请重新登录！`
                });
                this.editPwddialog = false;
                this.$refs.headerTop.$refs.editPwdform.resetFields();
                localUitl.removeSessionItem("userData");
                this.$router.push('/');
              } else {
                this.$message({
                  type: 'error',
                  message: `${res.data.message}!`
                });
              }
            });
          }
        })
      }
    },
    destroyed() {

    }
  }
</script>

<style scoped lang="less">

</style>
